﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SpringStudio.DAL
{
    /// <summary>
    /// 执行Sql语句的静态类
    /// </summary>
    public static class SqlHelper
    {
        private static IDataBase db = new DbFactory().Create();

        #region ExecuteDataSet

        /// <summary>
        /// 返回一个DataSet对象的SQL查询
        /// </summary>
        /// <param name="connection">数据库连接对象</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <param name="commandParameters">查询参数（IDbDataParameter数组）</param>
        /// <returns>DataSet对象</returns>
        public static DataSet ExecuteDataSet(IDbConnection connection, CommandType commandType, string commandText, params IDbDataParameter[] commandParameters)
        {
            return db.ExecuteDataSet(connection, commandType, commandText, commandParameters);
        }

        /// <summary>
        /// 返回一个DataSet对象的SQL查询
        /// </summary>
        /// <param name="connection">数据库连接对象</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <returns>DataSet对象</returns>
        public static DataSet ExecuteDataSet(IDbConnection connection, CommandType commandType, string commandText)
        {
            return db.ExecuteDataSet(connection, commandType, commandText);
        }

        /// <summary>
        /// 返回一个DataSet对象的SQL查询
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <param name="commandParameters">查询参数（IDbDataParameter数组）</param>
        /// <returns>DataSet对象</returns>
        public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText, params IDbDataParameter[] commandParameters)
        {
            return db.ExecuteDataSet(connectionString, commandType, commandText, commandParameters);
        }

        /// <summary>
        /// 返回一个DataSet对象的SQL查询
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <returns>DataSet对象</returns>
        public static DataSet ExecuteDataSet(string connectionString, CommandType commandType, string commandText)
        {
            return db.ExecuteDataSet(connectionString, commandType, commandText);
        }

        /// <summary>
        /// 返回一个DataSet对象的SQL查询
        /// </summary>
        /// <param name="transaction">数据库事务对象</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <param name="commandParameters">查询参数（IDbDataParameter数组）</param>
        /// <returns>DataSet对象</returns>
        public static DataSet ExecuteDataSet(IDbTransaction transaction, CommandType commandType, string commandText, params IDbDataParameter[] commandParameters)
        {
            return db.ExecuteDataSet(transaction, commandType, commandText, commandParameters);
        }

        /// <summary>
        /// 返回一个DataSet对象的SQL查询
        /// </summary>
        /// <param name="transaction">数据库事务对象</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <returns>DataSet对象</returns>
        public static DataSet ExecuteDataSet(IDbTransaction transaction, CommandType commandType, string commandText)
        {
            return db.ExecuteDataSet(transaction, commandType, commandText);
        }

        #endregion

        #region ExecuteNonQuery

        /// <summary>
        /// 更新数据库相关的SQL查询
        /// </summary>
        /// <param name="connection">数据库连接对象</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <param name="commandParameters">查询参数（IDbDataParameter数组）</param>
        /// <returns>影响的行数</returns>
        public static int ExecuteNonQuery(IDbConnection connection, CommandType commandType, string commandText, params IDbDataParameter[] commandParameters)
        {
            return db.ExecuteNonQuery(connection, commandType, commandText, commandParameters);
        }

        /// <summary>
        /// 更新数据库相关的SQL查询
        /// </summary>
        /// <param name="connection">数据库连接对象</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <returns>影响的行数</returns>
        public static int ExecuteNonQuery(IDbConnection connection, CommandType commandType, string commandText)
        {
            return db.ExecuteNonQuery(connection, commandType, commandText);
        }

        /// <summary>
        /// 更新数据库相关的SQL查询
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <param name="commandParameters">查询参数（IDbDataParameter数组）</param>
        /// <returns>影响的行数</returns>
        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params IDbDataParameter[] commandParameters)
        {
            return db.ExecuteNonQuery(connectionString, commandType, commandText, commandParameters);
        }

        /// <summary>
        /// 更新数据库相关的SQL查询
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <returns>影响的行数</returns>
        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
        {
            return db.ExecuteNonQuery(connectionString, commandType, commandText);
        }

        /// <summary>
        /// 更新数据库相关的SQL查询
        /// </summary>
        /// <param name="transaction">数据库事务对象</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <param name="commandParameters">查询参数（IDbDataParameter数组）</param>
        /// <returns>影响的行数</returns>
        public static int ExecuteNonQuery(IDbTransaction transaction, CommandType commandType, string commandText, params IDbDataParameter[] commandParameters)
        {
            return db.ExecuteNonQuery(transaction, commandType, commandText, commandParameters);
        }

        /// <summary>
        /// 更新数据库相关的SQL查询
        /// </summary>
        /// <param name="transaction">数据库事务对象</param>
        /// <param name="commandType">命令类型（Sql语句为Text）</param>
        /// <param name="commandText">Sql语句字符串</param>
        /// <returns>影响的行数</returns>
        public static int ExecuteNonQuery(IDbTransaction transaction, CommandType commandType, string commandText)
        {
            return db.ExecuteNonQuery(transaction, commandType, commandText);
        }

        #endregion

        #region Transaction
        /// <summary>
        /// 请求一个数据库事务
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <returns>事务实例</returns>
        public static IDbTransaction GetTransaction(string connectionString)
        {
            return db.GetTransaction(connectionString);
        }

        /// <summary>
        /// 将事务实例提交
        /// </summary>
        /// <param name="transaction">事务实例</param>
        public static void CommitTransaction(IDbTransaction transaction)
        {
            db.CommitTransaction(transaction);
        }

        /// <summary>
        /// 将事务实例回滚
        /// </summary>
        /// <param name="transaction">事务实例</param>
        public static void RollbackTransaction(IDbTransaction transaction)
        {
            db.RollbackTransaction(transaction);
        }
        #endregion

        #region NewSqlParameter

        /// <summary>
        /// 新建一个Sql查询参数
        /// </summary>
        /// <param name="parameterName">参数名称</param>
        /// <param name="parameterValue">参数值</param>
        /// <param name="dbType">Sql参数类型</param>
        /// <returns>参数实例</returns>
        public static IDbDataParameter NewSqlParameter(string parameterName, object parameterValue, SqlDbType dbType)
        {
            return db.NewSqlParameter(parameterName, parameterValue, dbType);
        }

        /// <summary>
        /// 新建一个Sql查询参数
        /// </summary>
        /// <param name="parameterName">参数名称</param>
        /// <param name="parameterValue">参数值</param>
        /// <returns>参数实例</returns>
        public static IDbDataParameter NewSqlParameter(string parameterName, object parameterValue)
        {
            return db.NewSqlParameter(parameterName, parameterValue);
        }

        #endregion
    }
}
